﻿using System;
using System.Xml.Xsl;
using Demo.Services.View.Controllers;
using Ext.Net;
using Demo.Services.Contracts.DTO.Filters;
using Demo.CommonBase.DTO.Infos;
using Demo.Services.Contracts.DTO.Infos;
using Demo.CommonBase.Logging;
using Demo.CommonBase.View.Forms;
using Demo.CommonBase.Exceptions;
using System.Reflection;

namespace Demo.Web.Catalogs
{
    public partial class ResourceType : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
        protected void BtnSearch_Click(object sender, DirectEventArgs e)
        {
            try
            {
                Logger.Info();

                dsResourceType.RemoveAll();
                dsResourceType.Reload();

            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }

        }
        protected void BtnClean_Click(object sender, DirectEventArgs e)
        {
            try
            {
                Logger.Info();

                txtDescription.Text = string.Empty;
                txtCost.Text = string.Empty;

                dsResourceType.RemoveAll();
                dsResourceType.Reload();
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }

        }
        private ResourceTypeFilter GetFilter()
        {
            ResourceTypeFilter filter = new ResourceTypeFilter();
            filter.Description = txtDescription.Text;
            filter.Cost = txtCost.Text == string.Empty ? 0 : Convert.ToDecimal(txtCost.Text);
            return filter;
        }
        protected void dsResourceType_ReadData(object sender, StoreReadDataEventArgs e)
        {
            ResourceTypeFilter filter;
            ResourceTypeController controller;
            PaginationInfo resourceTypePaging;
            ResourceTypeResultInfo result;
            try
            {
                Logger.Info();
                filter = GetFilter();
                resourceTypePaging = new PaginationInfo();

                resourceTypePaging.PageNum = e.Start;
                resourceTypePaging.PageSize = e.Limit;
                DataSorter sort = e.Sort[0];
                resourceTypePaging.Sort = sort.Property;
                resourceTypePaging.Direction = sort.Direction.ToString(); ;
                SetValue("ResourceTypeFilter", filter);
                SetValue("ResourceTypePagin", resourceTypePaging);
                controller = new ResourceTypeController();
                controller.ExecuteService(this, "Services.GetResourceTypes");
                result = (ResourceTypeResultInfo)GetValue("ResourceTypeList");
                e.Total = result.TotalReg;
                gpResourceType.GetStore().DataSource = result.ResourceTypeList;


            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
        }
        protected void BtnNew_Click(object sender, DirectEventArgs e)
        {
            try
            {
                Logger.Info();

                ResourceTypeWindow.Title = "Nuevo Tipo de Recurso";
                ResourceTypeWindow.Show();
                ComponentLoader loader = new ComponentLoader();
                loader.AutoLoad = true;
                loader.Url = "ResourceTypeEdit.aspx";
                ResourceTypeWindow.LoadContent(loader);

            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }

        }
        
        protected void BtnDeleteConfirm_Click(object sender, DirectEventArgs e)
        {
            int id = 0;
            if (e.ExtraParams["id"] != null)
            {
                id = Convert.ToInt32(e.ExtraParams["id"]);
            }

            X.Msg.Confirm("Confirmacion", "Desea Borrar Registro?", new MessageBoxButtonsConfig
            {
                Yes = new MessageBoxButtonConfig
                {
                    Handler = string.Format("Demo.DoYes({0})", id),
                    Text = "Si"
                },
                No = new MessageBoxButtonConfig
                {
                    Handler = "Demo.DoNothing()",
                    Text = "No",
                }
            }).Show();
        }

        [DirectMethod]
        public void DoYes(int id)
        {
            ResourceTypeController controller;
            SetValue("Id", id);
            controller = new ResourceTypeController();
            controller.ExecuteService(this, "Services.DeleteResourceType");
            Notification.Show(new NotificationConfig
            {
                Title = "Información",
                Icon = Icon.Information,
                Html = "El registro fué borrado",
                HideDelay = 3000
            });
            
            dsResourceType.RemoveAll();
            dsResourceType.Reload();
        }

        [DirectMethod]
        public void DoNothing()
        {
            return;
        }
        [DirectMethod]
        public void SaveAndRefresh(object sender, DirectEventArgs e)
        {
            Notification.Show(new NotificationConfig
            {
                Title = "Información",
                Icon = Icon.Accept,
                Html = "El registro fué Guardado Exitosamente",
                HideDelay = 3000
            });
            dsResourceType.Reload();
        }
        protected void ToExcel(object sender, EventArgs e)
        {
            string json = GridData.Value.ToString();
            var eSubmit = new StoreSubmitDataEventArgs(json, null);
            var xml = eSubmit.Xml;

            Response.Clear();
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("Content-Disposition", "attachment; filename=TipoRecurso.xls");
            var xtExcel = new XslCompiledTransform();
            xtExcel.Load(Server.MapPath("..\\ExportConfig\\Excel.xsl"));
            xtExcel.Transform(xml, null, Response.OutputStream);
            Response.End();
        }
    }
}